package com.example.message.services.Impl;

import com.example.message.common.StringUtil;
import com.example.message.common.util;
import com.example.message.dao.JMessageUtf8Mapper;
import com.example.message.dao.MessageUtf8PageLinkMapper;
import com.example.message.po.JMessageUtf8;
import com.example.message.po.MessageUtf8PageLink;
import com.example.message.services.messageService;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * @Comment：
 * @Author: 邹渺
 * @Date: 2019/3/20
 * @Time: 11:41
 */
@Service
public class messageServiceImpl implements messageService {

    private static final String[] letter = new String[]{"A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b",
            "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
            "x", "y", "z"};

    private static Random random = new Random();

    @Autowired
    private JMessageUtf8Mapper jMessageUtf8Mapper;

    @Autowired
    private MessageUtf8PageLinkMapper messageUtf8PageLinkMapper;

    @Override
    public void getMessageSql(Set<String> valueCn) {

        List<JMessageUtf8> jMessageUtf8List = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        Set<String> nameStrings = new HashSet<>();
        params.put("valueCn","已刊出");
        List<JMessageUtf8> jMessageUtf8s = jMessageUtf8Mapper.selectByMap(params);
        if(jMessageUtf8s == null || jMessageUtf8s.size() == 0){
            System.out.println("没有 ");
        }else{
            for (JMessageUtf8 jMessageUtf8 :jMessageUtf8s){
                if(jMessageUtf8.getName().startsWith("JCP") || jMessageUtf8.getName().startsWith("SCP")){

                }else{
                    System.out.println(jMessageUtf8);
                    jMessageUtf8List.add(jMessageUtf8);

                    nameStrings.add(jMessageUtf8.getName());
                }
            }
        }


    }

    @Override
    public  List<String> getmessageLinkSql(Set<String> set, String pageId, String pageName,boolean whetherToInsert) {
        List<String> messages = new ArrayList<>();
        for(String s :set){
            MessageUtf8PageLink messageUtf8PageLink = new MessageUtf8PageLink();
            messageUtf8PageLink.setMessageName(s);
            messageUtf8PageLink.setPageId(pageId);
            messageUtf8PageLink.setPageName(pageName);

            Long count = messageUtf8PageLinkMapper.selectCountAB(messageUtf8PageLink);

            if(count ==0){
                int i = 1;
                // messageUtf8PageLinkMapper.insert(messageUtf8PageLink);
                if(i ==1){
                    String message = "insert into jx3_message_utf8_page_link (page_id,message_name, page_name) values ('"+pageId+"', '"+s+"','"+pageName+"' );";
                    messages.add(message);
                }else {
                    System.out.println("当前i的 值是"+i+"meesage 是"+s);
                }
            }else {
                System.out.println("已经存在关联name:" + s);
                int i = 1;
                // messageUtf8PageLinkMapper.insert(messageUtf8PageLink);
                if (i == 1) {
                    String message = "insert into jx3_message_utf8_page_link (page_id,message_name, page_name) values ('" + pageId + "', '" + s + "','" + pageName + "' );";
                    String message2 = "insert into jx3_message_utf8_page_link (page_id,message_name, page_name)" +
                            "SELECT '" + pageId + "', '" + s + "' ,'" + pageName + "' " +
                            "FROM DUAL where not exists " +
                            "(SELECT * FROM jx3_message_utf8_page_link where " +
                            "page_id='" + pageId + "' and message_name= '" + s + "');";
                    messages.add(message2);
                } else {
                    System.out.println("当前i的 值是" + i + "meesage 是" + s);
                }
            }

        }
        System.out.println("");
        for( String s :messages){
            System.out.println(s);
        }
        return  messages;
    }

    @Override
    public Map<String, String> messageMap(Set<String> set,Set<String> sqlString,Set<String> names,Set<String> callSqls) {
        // 错误的 list
        Map<String, String> messageMap = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        for(String s :set){
            if(s.length() >0 && !"微软雅黑".equals(s) && ! "：".equals(s) && StringUtil.isNotBlank(s)){
                params.put("valueCn",s);
                List<JMessageUtf8> jMessageUtf8s = jMessageUtf8Mapper.selectByMap(params);
                if(jMessageUtf8s == null || jMessageUtf8s.size() == 0){
                    JMessageUtf8 jMessageUtf8=  determineIfTheMessageExists(s);
                    jMessageUtf8.setValueCn(s);
                    messageMap.put(s, jMessageUtf8.getName());

                    String valueEn = "NULL";
                    if(StringUtil.isBlank(jMessageUtf8.getValueEn())){
                        valueEn = "NULL";
                    }else{
                        int indexOf = jMessageUtf8.getValueEn().indexOf("'");
                        if(indexOf<0){
                            valueEn = "'"+jMessageUtf8.getValueEn()+"'";
                        }else{
                            valueEn = "\""+jMessageUtf8.getValueEn()+"\"";
                        }
                    }
                  //  String messagesql = "insert into jx3_j_message_utf8 (journal_id, value_en, value_cn,name) values (0, "+valueEn+", '"+s+"', '"+jMessageUtf8.getName()+"');";


                    String callSql = "CALL message(  '"+jMessageUtf8.getName()+"','"+s+"',"+valueEn+");";


                    String utf8sql = "insert into jx3_j_message_utf8 (journal_id, value_en, value_cn,name) " +
                            "SELECT 0, "+valueEn+",'"+s+"'," +
                            " '"+jMessageUtf8.getName()+"' " +
                            "FROM DUAL where not exists " +
                            "(SELECT * FROM jx3_j_message_utf8 where name='"+jMessageUtf8.getName()+"');";
                   // jMessageUtf8Mapper.insert(jMessageUtf8);

                    callSqls.add(utf8sql);
                   // sqlString.add(messagesql);
                    names.add(jMessageUtf8.getName());
                }else{
                    int i = 0;
                    for (JMessageUtf8 jMessageUtf8 :jMessageUtf8s){
                        // scp 的 message
                       /* if(jMessageUtf8.getName().startsWith("SCP")){
                            messageMap.put(s, jMessageUtf8.getName());
                            names.add(jMessageUtf8.getName());
                            break;
                        }else{
                            i++;
                        }*/
                        if(jMessageUtf8.getName().startsWith("JCP") || jMessageUtf8.getName().startsWith("SCP")){
                            i++;
                        }else{
                            messageMap.put(s, jMessageUtf8.getName());
                            names.add(jMessageUtf8.getName());
                            break;
                        }
                    }
                    if(i == jMessageUtf8s.size()){
                        JMessageUtf8 jMessageUtf8=  determineIfTheMessageExists(s);
                        jMessageUtf8.setValueCn(s);
                        messageMap.put(s, jMessageUtf8.getName());
                        String valueEn = "NULL";
                        if(StringUtil.isBlank(jMessageUtf8.getValueEn())){
                            valueEn = "NULL";
                        }else{
                            int indexOf = jMessageUtf8.getValueEn().indexOf("'");
                            if(indexOf<0){
                                valueEn = "'"+jMessageUtf8.getValueEn()+"'";
                            }else{
                                valueEn = "\""+jMessageUtf8.getValueEn()+"\"";
                            }
                        }
                      //  String messagesql = "insert into jx3_j_message_utf8 (journal_id, value_en, value_cn,name) values (0, "+valueEn+", '"+s+"', '"+jMessageUtf8.getName()+"');";

                        String callSql = "CALL message('"+jMessageUtf8.getName()+"','"+s+"',"+valueEn+");";
                       //  jMessageUtf8Mapper.insert(jMessageUtf8);

                        String utf8sql = "insert into jx3_j_message_utf8 (journal_id, value_en, value_cn,name) " +
                                "SELECT 0, "+valueEn+",'"+s+"'," +
                                " '"+jMessageUtf8.getName()+"' " +
                                "FROM DUAL where not exists " +
                                "(SELECT * FROM jx3_j_message_utf8 where name='"+jMessageUtf8.getName()+"');";

                        //sqlString.add(messagesql);
                        names.add(jMessageUtf8.getName());
                        callSqls.add(utf8sql);
                    }
                }
            }



        }
        return messageMap;
    }

    private JMessageUtf8 determineIfTheMessageExists(String valueCn){
        Map<String, Object> params = new HashMap<>();
        params.put("valueCn",valueCn);
        List<JMessageUtf8> jMessageUtf8s = jMessageUtf8Mapper.selectByMap(params);
        if(jMessageUtf8s == null || jMessageUtf8s.size() == 0){

             String valueEN  = onlineTranslationAssemblymessage( valueCn);

            String name = getwebMessageName(valueEN);
            JMessageUtf8 selectjMessageUtf8 = getmessageByname(name, valueCn);
            selectjMessageUtf8.setValueEn(valueEN);
            return selectjMessageUtf8;
        }else{
            int i = 0;
            for (JMessageUtf8 jMessageUtf8 :jMessageUtf8s){

                // scp 的 message
                /*if(jMessageUtf8.getName().startsWith("SCP")){
                    return jMessageUtf8;
                }
                i++;*/
                if(jMessageUtf8.getName().startsWith("JCP") || jMessageUtf8.getName().startsWith("SCP")){
                    i++;
                }else{
                    return jMessageUtf8;
                }
                if(i == jMessageUtf8s.size()){
                    String valueEN  = onlineTranslationAssemblymessage( valueCn);
                    String name = getwebMessageName(valueEN);
                    JMessageUtf8 selectjMessageUtf8 = getmessageByname(name, valueCn);
                    selectjMessageUtf8.setValueEn(valueEN);
                    return selectjMessageUtf8;
                }
            }
        }
        return null;
    }

    private JMessageUtf8 getmessageByname(String name ,String value){
        JMessageUtf8 jMessageUtf8 = new JMessageUtf8();
        Map<String, Object> params = new HashMap<>();
        params.put("name",name);
        List<JMessageUtf8> jMessageUtf8s = jMessageUtf8Mapper.selectByMap(params);
        if(jMessageUtf8s == null || jMessageUtf8s.size() == 0){
            jMessageUtf8.setName(name);
            jMessageUtf8.setValueCn(value);
            return jMessageUtf8;
        }else{
            for (JMessageUtf8 jMessageUtf81 :jMessageUtf8s){
                if(jMessageUtf81.getName().startsWith("JCP") || jMessageUtf81.getName().startsWith("SCP")){

                }else{
                    int iu = random.nextInt(52);
                    String s= letter[iu];
                    name = name + s;
                    return getmessageByname( name,  value);
                }
            }
        }


        return jMessageUtf8;
    }


    private String onlineTranslationAssemblymessage(String valueCn){
        String name = "";

        String s = util.translatedChineseCharacters(valueCn);
        //name = util.underline2Camel(s);
        //name = "WEB" + name;
        System.out.println("valueEn:" + s);
        return s;
    }


    private String getwebMessageName(String valueEn){
        String name  = util.underline2Camel(valueEn);
        name = "WEB" + name;
        if(name.length()>150){
            name.indexOf(0, 150);
        }
        System.out.println(name);
        return name;
    }
}